﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Streambolics.Gui
{
    /// <summary>
    ///     The base class for all forms in Streambolics
    ///     applications.
    /// </summary>

    public partial class StandardForm : Form
    {
        private static StandardForm _MainForm;

        public StandardForm ()
        {
            InitializeComponent ();
        }

        #region Main Form

        /// <summary>
        ///     Register this window as the main application window.
        /// </summary>
        /// <remarks><para>
        ///     The main application window should register itself as soon as possible,
        ///     ideally in its constructor, or the OnLoad event.
        /// </para><para>
        ///     Only one window in the application can register itself as the main window.
        /// </para><para>
        ///     The MainForm class registers itself automatically as the main form.
        /// </para></remarks>
        /// <seealso cref="Streambolics.App.MainForm"/>

        protected void RegisterAsMain ()
        {
            // TODO : Check here that there is no other window already registered.

            _MainForm = this;
        }

        /// <summary>
        ///     The registered main window.
        /// </summary>

        public static StandardForm MainForm
        {
            get
            {
                return _MainForm;
            }
        }

        #endregion

        partial void OnThisBeginLoading (EventArgs e)
        {
            if (_MainForm == this)
            {
                Cursor = Cursors.WaitCursor;
                Application.DoEvents ();
            }
            else
            {
                MainBeginUpdate ();
            }
        }

        partial void OnThisEndLoading (EventArgs e)
        {
            if (_MainForm == this)
            {
                Cursor = Cursors.Default;
                Application.DoEvents ();
            }
            else
            {
                MainEndUpdate ();
            }
        }

        internal protected static void MainBeginUpdate ()
        {
            if (_MainForm != null)
            {
                _MainForm.BeginUpdate ();
            }
        }

        internal protected static void MainEndUpdate ()
        {
            if (_MainForm != null)
            {
                _MainForm.EndUpdate ();
            }
        }

        protected override void OnLoad (EventArgs e)
        {
            base.OnLoad (e);
            ObservableObjects.Add (this, Name, "Forms");
        }
    }
}
